﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FoundationLib.Ifc.DependencyInjection.Configuration
{
	/// <summary>
	/// Represents a collection of <see cref="IocConfigElement"/> items.
	/// </summary>
	public class IocConfigElementCollection : ConfigurationElementCollection, IEnumerable<IocConfigElement>
	{
		/// <summary>
		/// Creates a new <see cref="IocConfigElementCollection"/> instance.
		/// </summary>
		public IocConfigElementCollection()
			: base()
		{
		}

		/// <summary>
		/// Gets the default element from the collection or null if no such element exists
		/// </summary>
		public IocConfigElement Default
		{
			get
			{
				return this.Where(x => x.IsDefault).FirstOrDefault();
			}
		}

		/// <summary>
		/// Gets an element from the collection.
		/// </summary>
		/// <param name="name">The name of the element</param>
		/// <returns>The element with the specified name</returns>
		public new IocConfigElement this[string name]
		{
			get
			{
				return this.Where(x => x.Name == name).FirstOrDefault();
			}
		}

		/// <summary>
		/// Gets an element from the collection.
		/// </summary>
		/// <param name="index">The zero-based index of the element to retrieve</param>
		/// <returns>The element at the specified index</returns>
		public IocConfigElement this[int index]
		{
			get
			{
				return this.BaseGet(index) as IocConfigElement;
			}
		}

		/// <see cref="ConfigurationElementCollection.CreateNewElement()"/>
		protected override ConfigurationElement CreateNewElement()
		{
			return new IocConfigElement();
		}

		/// <see cref="ConfigurationElementCollection.GetElementKey(ConfigurationElement)"/>
		protected override object GetElementKey(ConfigurationElement element)
		{
			throw new NotImplementedException();
		}

		/// <see cref="IEnumerable{T}.GetEnumerator()"/>
		IEnumerator<IocConfigElement> IEnumerable<IocConfigElement>.GetEnumerator()
		{
			for (int i = 0; i < this.Count; i++)
				yield return this[i];
		}
	}
}
